﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Messaging;
using System.Text;
using System.Threading.Tasks;

namespace FremtidensPlejehjem_Server.Messaging
{
    public class GeneralMessaging
    {
        MessageQueue _queueReceiveInfo;
        MessageQueue _queueSendInfo;

        MessageQueue _queueReceiveMap;
        MessageQueue _queueSendMap;

        public GeneralMessaging()
        {
            _queueReceiveInfo = CreateQueue("info-to-server");
            _queueSendInfo = CreateQueue("server-to-info");

            _queueReceiveMap = CreateQueue("map-to-server");
            _queueSendMap = CreateQueue("server-to-map");
        }

        public void InitializeMessaging()
        {
            Consumer consumeFromInfo = new Consumer(_queueReceiveInfo, _queueSendInfo);
            Console.WriteLine("The consumer from InfoScreen has been created");
            Consumer consumeFromMap = new Consumer(_queueReceiveMap, _queueSendMap);
            Console.WriteLine("The consumer from MapScreen has been created");
        }

        private MessageQueue CreateQueue(string queueName)
        {
            // Create queue
            string path = @".\Private$\" + queueName;
            MessageQueue mq = new MessageQueue();

            if (MessageQueue.Exists(path))
                mq = new MessageQueue(path);
            else
                mq = MessageQueue.Create(path);

            return mq;
        }

        public MessageQueue GetChannelByName(string channelName)
        {
            return new MessageQueue(@".\Private$\" + channelName);
        }
    }
}
